package com.ipartek.formacion.whoiswho.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class InicioServlet
 */
public class InicioServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    @Override
    protected void doGet(HttpServletRequest request,
	    HttpServletResponse response) throws ServletException, IOException {
	doProcess(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    @Override
    protected void doPost(HttpServletRequest request,
	    HttpServletResponse response) throws ServletException, IOException {
	doProcess(request, response);
    }

    /**
     *
     * @param request
     * @param response
     */
    protected void doProcess(HttpServletRequest request,
	    HttpServletResponse response) {

	try {
	    if (comprobarUsuario()) {
		request.getRequestDispatcher("busqueda.jsp").forward(request,
			response);
		;

	    } else {
		request.getRequestDispatcher("registro.jsp").forward(request,
			response);
		;
	    }
	} catch (Exception ex) {
	    ex.printStackTrace();
	}

    }

    // Funcion provisional para hacer la redireccion
    // TODO Hay que recoger el usuario de la base de datos

    private boolean comprobarUsuario() {
	boolean resul = false;
	String username = System.getProperty("user.name");
	if (username.equalsIgnoreCase("curso")) {
	    resul = true;
	}

	return resul;
    }
}
